/********************************************************************/
/*  Copyright (C) SSE-USTC, 2013                                    */
/*                                                                  */
/*  FILE NAME             :  ReadandParse.c                         */
/*  PRINCIPAL AUTHOR      :  Tangxingyu                             */
/*  SUBSYSTEM NAME        :  main                                   */
/*  MODULE NAME           :  Restore                                */
/*  LANGUAGE              :  C                                      */
/*  TARGET ENVIRONMENT    :  Linux                                  */
/*  DATE OF FIRST RELEASE :  2013/5/15                              */
/*  DESCRIPTION           :  The main of mail restore               */
/********************************************************************/

/*
 * Revision log:
 *
 * Created by Tangxingyu ,2012/5/15
 *
 */

#define DEBUG 0 //0-关闭调试信息 1-打开调试信息
#include <stdio.h>
#include "mem_manage.h"
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "Filter.h"
#include "threadpool.h"
typedef unsigned int u_int;

u_int strtonum2(u_char * cnum,u_int low,u_int high);

//还原模块主函数
int main()
{
    int mailcount = 0;
    int semid;
	int readsize;
	void *smptr;    //共享内存地址
	unsigned int mailsize = 0;
    int index = 0;
	char * mailtmpsave = NULL;
	u_char *head = NULL;        //存放开始的16字节内容用来存放邮件IP信息以及邮件长度字段
    threadjob * job = NULL;     //threadjob point
    //create thread pool
    if(Theadpool_create(5)!=0)
    {
        printf("create threadpool failed\n");
    }
	semid=getSem(M_SEM_NAME,M_SEM_ID,GET);               //共享内存id号
	mem_start=getMem(M_MEM_NAME,M_MEM_ID,TOTAL_LEN,GET); //打开共享内存
	smptr=mem_start;                                     //共享内存链接

    printf("Ready for Readmail\n");
    for(mailcount = 1; ; )   //循环等待读取
    {
        while(1)
        {
#if DEBUG
            printf("The %d Mail coming\n",mailcount);
#endif
            for(;;) //读第一次
            {
                head = (u_char *)malloc(20*sizeof(u_char));
                assert(head!=NULL);
                memset(head,0,20);
                readsize = my_read(&smptr,head,semid);  //读取首部信息
                //循环等待读取首部信息
                if(readsize == READ_FAILED)
                {
#if DEBUG
                    printf("read wait~\n");
#endif
                    sleep(1);
                }
                else if(readsize == 0)
                {
                    printf("read 0 char~\n");
                    sleep(1);
                }
                else
                {
                    //printf("\n------------------------Begin Read MailHead-------------------------------\n");
                    mailsize = strtonum2(head,12,15);
                    printf("mailsize is %u\n",mailsize);
                    break;
                }
            }
            for(; ;)    //读第二次
            {
                mailtmpsave = NULL;
                mailtmpsave = (char *)malloc((mailsize)*sizeof(char));
                assert(mailtmpsave!=NULL);
                memset(mailtmpsave,0,mailsize);
                readsize = my_read(&smptr,mailtmpsave,semid);   //读取邮件正文信息
                if(readsize == READ_FAILED)
                {
                    printf("read mail fail then sleep\n");
                    free(mailtmpsave);
                    sleep(1);
                }
                else if(readsize == 0)
                {
                    printf("read 0 char of mailcontent~\n");
                    free(mailtmpsave);
                    sleep(1);
                }
                else
                {

                    printf("\n-----------------------Read mailcontent is----------------------\n");
                    write_sharemem(mailtmpsave,readsize,mailcount);
#if DEBUG
                    printf("\n-------------------------Mail_16--------------------------------\n");
                    for (index = 0; index < readsize; ++index)
                    {
                        printf("%02x",mailtmpsave[index] );
                    }
                    printf("\n-------------------------Mail_16--------------------------------\n");
#endif
                    break;
                }
            }
            job = NULL;
            job = (threadjob*)malloc(sizeof(threadjob));
            assert(job!=NULL);
            job->head = head;
            job->mailtmpsave = mailtmpsave;
            job->mailsize = mailsize;
            Threadpool_add_job((void *)job);
            printf("Read one of MailALL OK~\n");
            printf("This is %d Mail~\n",mailcount);
            mailcount++;
        }
    }
    Threadpool_destroy();
    return 0;
}






